// pages/my/my-core.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    exchange: 0,

    card_id: -1,

    code: '',
  },

  bindCard(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var fee = e.currentTarget.dataset.fee;
    that.setData({
      card_id: id,
      fee: fee
    })
  },

  bindConfirm(e) {
    var that = this;
    if (that.data.code == '') {
      wx.showToast({
        title: '请输入正确的兑换码',
        duration: 1300,
        icon: "none",
        mask: true,
      })
    } else {
      wx.showLoading({
        title: '提交加载中',
        mask: true,
      })
      wx.request({
        url: app.globalData.url + 'RechargeCheck',
        data: {
          code: that.data.code,
        },
        method: 'POST',
        success: function (res) {
          that.setData({
            card_list: res.data.info.code,
            exchange: 1,
            status: res.data.status
          })
          wx.hideLoading()
        },
      })
    }
  },

  bindRechareDo(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showLoading({
      title: '提交加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'RechareDo',
      data: {
        code: that.data.code,
        uid: app.globalData.uid,
        id: id
      },
      method: 'POST',
      success: function (res) {
        if (res.data.status == 1) {
          wx.showToast({
            title: '兑换成功',
            duration: 1300,
            icon: "none",
            mask: true,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 1300,
            icon: "none",
            mask: true,
          })
        }
        that.setData({
          exchange: 0
        })
        wx.hideLoading()
      },
    })
  },

  bindConfirmTo(e) {
    var that = this;
    that.setData({
      exchange: 0
    })
  },

  bindcode(e) {
    var that = this;
    that.setData({
      code: e.detail.value.trim()
    })
  },

  bindApplyvip(e) {
    const that = this;
    var vip_id = e.currentTarget.dataset.vipid;
    wx.showLoading({
      title: '提交加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'Applyvip',
      data: {
        uid: app.globalData.uid,
        vip_id: vip_id,
      },
      method: 'POST',
      success: function (res) {
        if (res.data.info == true) {
          that.bindVipcenter()
          wx.showToast({
            title: '申请成功',
            duration: 1200,
            icon: "loading",
            mask: true,
          })
        }
        wx.hideLoading()
      },
    })
  },

  bindUsetInfo(e) {
    const that = this;
    if (e.detail.userInfo) {
      that.setData({
        userInfo: e.detail.userInfo
      })
      app.globalData.userInfo = e.detail.userInfo;
      wx.request({
        url: app.globalData.url + 'Obtain',
        data: {
          openid: app.globalData.openid,
          wx_name: app.globalData.userInfo.nickName,
          wx_img: app.globalData.userInfo.avatarUrl,
        },
        method: 'POST',
        success: function (res) {
          that.bindOrders_submit()
        },
      })
    }
  },

  bindOrders_submit(e) {
    var that = this;
    wx.showLoading({
      title: '下单中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'Card_submit',
      data: {
        uid: app.globalData.uid,
        rid: that.data.card_id,
        fee: that.data.fee,
      },
      method: 'POST',
      success: function (res) {
        var oid = res.data.out_trade_no;
        if (res.data.state == 1) {
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            fail: (res) => {
              wx.request({
                url: app.globalData.url + 'cardpayfales',
                data: {
                  oid: oid,
                },
                method: 'POST',
                success: function (res) {
                  if (res.data.info) {
                    wx.showToast({
                      title: '支付取消',
                      icon: "loading",
                      duration: 1200,
                      mask: true,
                    })
                  }
                }
              })
            },
            success: (res) => {
              wx.request({
                url: app.globalData.url + 'cardpayok',
                data: {
                  oid: oid,
                },
                method: 'POST',
                success: function (res) {
                  wx.navigateTo({
                    url: '../card/card',
                    success: function (res) {},
                  })
                }
              })
            },
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1200,
            mask: true,
          })
        }
        wx.hideLoading()
      },
    })
  },

  navTo(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  bindVipcenter(e) {
    const that = this;
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'Rechargepage',
      method: 'POST',
      data: {
        uid: app.globalData.uid,
      },
      success: function (res) {
        if (res.data.info) {
          that.setData({
            info: res.data.info
          })
        }
        wx.hideLoading()
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    if (options.card_id == 1) {
      that.setData({
        card_id: options.card_id
      })
    }
    that.bindVipcenter();
    wx.stopPullDownRefresh();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.bindVipcenter();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})